import time

import pygame
import os
import random
from pygame.locals import *
# url1=os.path.abspath(os.path.dirname(__file__))
# print(url1)
#定义一个飞机的类：
class Heroplane(object):
    def __init__(self,screen):
        '''
        初始化函数
        :param screen: 主窗口对象
        '''
        self.x=150
        self.y=450
        self.screen=screen
        self.imagepath=('/Users/didi1/PycharmProjects/djangoProject/飞机大战游戏/feiji的副本/hero.jpg')
        self.image=pygame.image.load(self.imagepath)
        self.bulletlist=[]                      #定义一个子弹列表
        pass
    def display(self):
        '''
        在主窗口显示飞机
        :return:
        '''
        self.screen.blit(self.image,(self.x,self.y))        #    显示飞机
        needdelitemlist=[]                                  #       需要删除的子弹类
        for item in self.bulletlist:
            if item.judge():
                needdelitemlist.append(item)
                pass
        for i in needdelitemlist:
            needdelitemlist.remove(i)
            pass
        for bullet in self.bulletlist:
            bullet.display()
            bullet.move()
        pass
    def moverleft(self):
        '''
        向左移动
        :return:
        '''
        if self.x>0:
            self.x -=10
            pass
        pass
    def moverright(self):
        if self.x<310:
            self.x +=10
            pass
        pass
    def shebullet(self):                    #  发射子弹
        newbullet=Bullet(self.x,self.y,self.screen)
        self.bulletlist.append(newbullet)
#创建一个子弹类
class Bullet(object):
    def __init__(self,x,y,screen):
        '''

        :param x: 左右的位置
        :param y: 上下的位置
        :param screen: 窗体
        '''
        self.x=x+13
        self.y=y-20
        self.screen=screen
        self.imagege=pygame.image.load('/Users/didi1/PycharmProjects/djangoProject/飞机大战游戏/feiji的副本/bullet.jpg')
        pass
    def display(self):
        self.screen.blit(self.imagege,(self.x,self.y))
        pass
    def move(self):
        self.y-=5
        pass
    def judge(self):
        if self.y<0:
            return True
        else:
            return False
        pass
    pass
#定义一个敌机类：
class Enemyplane(object):
    def __init__(self,screen):
        self.direction='right'
        self.x=0
        self.y=0
        self.screen=screen
        self.imagepath=('/Users/didi1/PycharmProjects/djangoProject/飞机大战游戏/feiji的副本/enemy0.jpg')
        self.image=pygame.image.load(self.imagepath)
        self.bulletlist=[]
        pass
    def display(self):
        self.screen.blit(self.image,(self.x,self.y))
        needdelitemlist = []  # 需要删除的子弹类
        for item in self.bulletlist:
            if item.judge():
                needdelitemlist.append(item)
                pass
        for i in needdelitemlist:
            needdelitemlist.remove(i)
            pass
        for Enemybullet in self.bulletlist:
            Enemybullet.display()
            Enemybullet.move()
        pass
    def shebullet(self):
        num=random.randint(1,30)
        if num==3:
            newbullet=Enemybullet(self.x,self.y,self.screen)
            self.bulletlist.append(newbullet)
            pass
        pass
    def move(self):
        if self.direction=='right':
            self.x+=3
            pass
        elif self.direction=='left':
            self.x-=3
        if self.x<0:
            self.direction='right'
            pass
        elif self.x>(350-20):
            self.direction = 'left'
            pass
#定义敌方发射的子弹类
class Enemybullet(object):
    def __init__(self,x,y,screen):
        self.x=x
        self.y=y+10
        self.screen=screen
        self.imagege=pygame.image.load('/Users/didi1/PycharmProjects/djangoProject/飞机大战游戏/feiji的副本/bullet1.jpg')
        pass
    def display(self):
        self.screen.blit(self.imagege,(self.x,self.y))
        pass
    def move(self):
        self.y+=2
        pass
    def judge(self):
        if self.y>500:
            return True
        else:
            return False
        pass
    pass
#键盘检测：
def key_control(Heroobj):
    '''
    定义一个普通的函数来进行键盘检测
    :param Heroobj: 可控制检测的对象
    :return:
    '''
    eventlist=pygame.event.get()             #得到键盘内容
    for event in eventlist:
        if event.type==QUIT:
            print('退出游戏')
            exit()
            pass
        elif event.type==KEYDOWN:
            if event.key==K_a or event.key==K_LEFT:
                print('left')
                Heroobj.moverleft()              #调用左移方法
                pass
            elif event.key==K_d or event.key==K_RIGHT:
                print('right')
                Heroobj.moverright()              #调用右移方法
                pass
            elif event.key==K_SPACE:
                print('空格')
                Heroobj.shebullet()
            pass
        pass
#定义一个主函数：
def main():
    background=pygame.image.load('/Users/didi1/PycharmProjects/djangoProject/飞机大战游戏/feiji的副本/background.png')
    pygame.display.set_caption('飞机大战小游戏')
    pygame.mixer.init()
    pygame.mixer.music.load('/Users/didi1/PycharmProjects/djangoProject/飞机大战游戏/feiji的副本/background.mp3')
    pygame.mixer.music.set_volume(0.3)
    pygame.mixer.music.play(-1)
    screen = pygame.display.set_mode((350, 500), depth=32)
    hero = Heroplane(screen)
    enmeyplane=Enemyplane(screen)
    while True:
        screen.blit(background,(0,0))
        hero.display()
        enmeyplane.display()
        enmeyplane.move()
        enmeyplane.shebullet()
        key_control(hero)
        pygame.display.update()
        pass
    pass
if __name__=='__main__':
    main()